
var localhost = '172.16.5.236'
// 查询
function AjaxQuery() {
    map.removeLayer(vector);
    name_place = jQuery("#select1  option:selected").text();
    name_id = jQuery("#select2  option:selected").text();
    if (name_place == "武汉") {
        let d = ol.proj.fromLonLat([114.5, 30.5]);
        this.map.getView().setCenter(d);
        this.map.getView().setZoom(10);
    } else if (name_place == "武昌区") {
        let d = ol.proj.fromLonLat([114.30, 30.57]);
        this.map.getView().setCenter(d);
        this.map.getView().setZoom(12);
    } else if (name_place == "洪山区") {
        let d = ol.proj.fromLonLat([114.33, 30.50]);
        this.map.getView().setCenter(d);
        this.map.getView().setZoom(12);
    } else if (name_place == "青山区") {
        let d = ol.proj.fromLonLat([114.38, 30.63]);
        this.map.getView().setCenter(d);
        this.map.getView().setZoom(12);
    } else if (name_place == "江岸区") {
        let d = ol.proj.fromLonLat([114.30, 30.60]);
        this.map.getView().setCenter(d);
        this.map.getView().setZoom(12);
    } else if (name_place == "江汉区") {
        let d = ol.proj.fromLonLat([114.27, 30.60]);
        this.map.getView().setCenter(d);
        this.map.getView().setZoom(12);
    } else if (name_place == "桥口区") {
        let d = ol.proj.fromLonLat([114.27, 30.57]);
        this.map.getView().setCenter(d);
        this.map.getView().setZoom(12);
    } else if (name_place == "汉阳区") {
        let d = ol.proj.fromLonLat([114.27, 30.55]);
        this.map.getView().setCenter(d);
        this.map.getView().setZoom(12);
    }

    var featureRequest = new ol.format.WFS().writeGetFeature({
        srsName: 'EPSG:4326',
        featureNS: 'http://localhost:8080/taxidm',
        featurePrefix: 'taxidm',
        featureTypes: ['trackpoints_test'],
        outputFormat: 'application/json',
        filter: ol.format.filter.equalTo('id', name_id)
    });

    fetch('http://'+localhost+':8080/geoserver/wfs', {
        method: 'POST',
        body: new XMLSerializer().serializeToString(featureRequest)
    }).then(function (response) {
        return response.json();
    }).then(function (json) {
        var features = (new ol.format.GeoJSON({
            featureProjection: 'EPSG:3857'
        })).readFeatures(json)
        console.log(features);
        if (features.length == 0) {
            alert('没有数据')
        } else {
            blur_new = document.getElementById('blur');
            radius_new = document.getElementById('radius');
            blur_new.addEventListener('input', function () {
                vector.setBlur(parseInt(blur_new.value, 5));
            });

            radius_new.addEventListener('input', function () {
                vector.setRadius(parseInt(radius_new.value, 10));
            });
            var source = new ol.source.Vector({
                features: features
            });
            vector_new = new ol.layer.Heatmap({
                source: source,
                blur: parseInt(blur_new.value, 10),
                radius: parseInt(radius_new.value, 10),
            });
            map.addLayer(vector_new);
        }
    });
}

// 清理图层
function ClearLayer() {
    map.removeLayer(vector);
    map.removeLayer(vector_new);
    map.removeLayer(drawVector);
}

// 多边形绘制
var feature;

function addInteraction() {
    var draw = new ol.interaction.Draw({
        source: drawSource,
        type: 'Polygon'
    });
    map.addInteraction(draw);
    draw.on('drawend', function (evt) {
        feature = evt.feature;
        map.removeInteraction(draw);
    })
}
//多边形查询
function polyQuery() {
    map.removeLayer(vector);
    var featureRequest = new ol.format.WFS().writeGetFeature({
        srsName: 'EPSG:3857',
        featureNS: 'http://localhost:8080/taxidm',//创建工作区时的命名空间
        featurePrefix: 'taxidm',//命名空间即工作区的名称
        featureTypes: ['trackpoints_test'],//图层的名称
        outputFormat: 'application/json',
        filter: ol.format.filter.intersects('geom', feature.getGeometry()) //第一个是数据的空间字段
        // filter: ol.format.filter.and(   //多条件过率
        //     ol.format.filter.equalTo('出租车i', '1015'),
        //     ol.format.filter.intersects("geom", feature.getGeometry())
        //  )
    });

    fetch('http://'+localhost+':8080/geoserver/wfs', {
        method: 'POST',
        body: new XMLSerializer().serializeToString(featureRequest)
    }).then(function (response) {
        return response.json();
    }).then(function (json) {
        var features = new ol.format.GeoJSON().readFeatures(json)
        if (features.length == 0) {
            alert('没有数据')
        } else {
            blur_new = document.getElementById('blur');
            radius_new = document.getElementById('radius');
            blur_new.addEventListener('input', function () {
                vector.setBlur(parseInt(blur_new.value, 5));
            });

            radius_new.addEventListener('input', function () {
                vector.setRadius(parseInt(radius_new.value, 10));
            });
            var source = new ol.source.Vector({
                features: features
            });
            vector_new = new ol.layer.Heatmap({
                source: source,
                blur: parseInt(blur_new.value, 10),
                radius: parseInt(radius_new.value, 10),
            });
            map.addLayer(vector_new);
        }
    });
}